<?php

namespace App\Jobs;

use App\Constant\QueueKey;
use App\Facades\CommonPlatformRequestFacade;
use App\Models\UserAuthSpiderTransLog;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;

class AliPayJob implements ShouldQueue {
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $tries = 3;

    public $timeout = 30;

    protected $spider;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(UserAuthSpiderTransLog $spider) {
        $this->spider = $spider;
        $this->onQueue(QueueKey::QUEUE_ALIPAY_SPIDER_RESULT);
    }


    public function handle() {
        $status = UserAuthSpiderTransLog::getStatusByRiskInfo($this->spider->trade_no, $this->spider, UserAuthSpiderTransLog::TYPE_ZHIMA);
        if ($status != UserAuthSpiderTransLog::STATUS_INIT) {
            \Log::debug('当前爬虫tradeNo为'.$this->spider->trade_no.'状态为' . $this->spider->status . '变更为' . $status);
            $this->spider->status = $status;
            $this->spider->save();
        }

        if ($this->spider->status == UserAuthSpiderTransLog::STATUS_FAILED) {
            Log::debug('risk_reptile_job', ['loan_request_id' => $this->spider->loan_request_id]);
            UserAuthSpiderTransLog::handleByStatus($this->spider->loan_request_id);
        }
    }
}
